package com.whater.auth.client.service;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.whater.auth.client.vo.PathVo;
import com.whater.common.util.StringUtils;

@Component
public class ImageService {
	
	@Value("${wechat.image.path}")
	private String basePath ;
	
	public List<PathVo> getPath(String path){
		String realPath = basePath ;
		if(StringUtils.isNotEmpty(path)) {
			realPath += "/"+ path;
		}
		return Arrays.asList(new File(realPath).listFiles()).stream().map(item->{
			return new PathVo(item.getAbsolutePath(),item.getName(),isDate(item.getName()));
		}).collect(Collectors.toList());
	}
	
	public List<String> getImage(String path,Integer skip,Integer limit){
		String realPath = basePath ;
		if(StringUtils.isNotEmpty(path)) {
			realPath += "/"+ path;
		}
		return Arrays.asList(new File(realPath).listFiles()).stream().skip(skip).limit(limit).map(File::getName).collect(Collectors.toList());
	}
	
	public String getFullPath(String image) {
		String realPath = basePath ;
		if(StringUtils.isNotEmpty(image)) {
			realPath += "/"+ image;
		}
		return realPath ;
	}
	public boolean isDate(String date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd");
			sdf.parse(date);
		} catch (ParseException e) {
			return false;
		}
        return true;
	}
}
